﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebStore.Domain.Entities.Model;
using WebStore.Web.App.Models.Cart;
using WebStore.Web.App.Utility.Enum;
using WebStore.Web.App.Utility.Other;

namespace WebStore.Web.App.Controllers
{
    public partial class CartController
    {
        public ViewResult Summary(string returnUrl)
        {
            return View(new SummaryModel
                            {
                                Cart = GetCart(),
                                ReturnUrl = returnUrl
                            });
        }

        public ViewResult CheckoutSummary(Cart cart)
        {
            return View(cart);
        }

        [Authorize]
        public ActionResult Checkout()
        {
            var cart = GetCart();
            if (cart.Orders.Any())
            {
                if (!_orderService.Checkout(CurrentUser.Id,GetCart().ToOrderDetails()))
                {
                    CreateMessage("Something goes wrong", ClientMessageType.Error);
                    return View();
                }
                cart.Clear();
            }
            else
            {
                CreateMessage("Cart is empty", ClientMessageType.Error);
            }
            return RedirectToAction("Orders", "Cart");
        }
    }
}